package com.zyk.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangsan
 * @date 2021/4/4 14:50
 */
public class C781 {


    /** https://leetcode-cn.com/problems/rabbits-in-forest/ */
    public static int numRabbits(int[] answers) {
        Map<Integer, Integer> count = new HashMap<Integer, Integer>();
        // 统计多少只兔子回答了y
        for (int y : answers) {
            count.put(y, count.getOrDefault(y, 0) + 1);
        }
        int ans = 0;
        for (Map.Entry<Integer, Integer> entry : count.entrySet()) {
            int y = entry.getKey(), x = entry.getValue();
            ans += (x + y) / (y + 1) * (y + 1);
        }
        return ans;
    }


    // for test
    public static void main(String[] args) {
        int[] answers = {1,0,1,0,0};
        System.out.println(numRabbits(answers));
    }

}
